package org.xteam.kenken;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class KenkenApplet extends JApplet {

	private static final long serialVersionUID = -6622113519279436522L;
	
	private KenKenPanel kenkenPanel;

	private void createGUI() {
		JPanel buttonPanel = new JPanel(new FlowLayout());
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		
		JButton newButton = new JButton("New");
		buttonPanel.add(newButton);
		newButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String value = JOptionPane.showInputDialog("Choose size", 6);
				int newSize = 6;
				try {
					newSize = Integer.parseInt(value);
				} catch (NumberFormatException e) {
				}
				kenkenPanel.setGridSize(newSize);
			}
		});
		JButton groupButton = new JButton("Make Cage");
		buttonPanel.add(groupButton);
		groupButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String value = JOptionPane.showInputDialog("Which operation?", "+10");
				kenkenPanel.makeGroup(value);
			}
		});	
	
		kenkenPanel = new KenKenPanel();
		getContentPane().add(kenkenPanel, BorderLayout.CENTER);
	
		addMouseListener(kenkenPanel);
	}
	
	public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) { 
            System.err.println("createGUI didn't successfully complete");
        }
	}
}
